<template>
  <div class="warp">
    <div class="warp_auto">
      <div class="warp_top">
        <div class="jiansuo-box">
          <span class="label">盒子IP：</span>
          <el-input
            v-model="boxIp"
            class="input"
            clearable
            placeholder="请输入盒子IP"
          />
        </div>

        <div class="jiansuo-box">
          <span class="label">盒子编号：</span>
          <el-input
            v-model="boxNumbers"
            class="input"
            clearable
            placeholder="请输入盒子ip"
          />
        </div>

        <div class="jiansuo-box">
          <span class="label">盒子状态：</span>
          <el-select
            v-model="value"
            placeholder="请选择状态"
            clearable
            class="input"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>
        <el-button
          type="primary"
          icon="el-icon-search"
          class="btn"
          @click="btn"
          >检索</el-button
        ><el-button
        type="primary"
        icon="el-icon-plus"
        class="btn"
        @click="handleAdd"
      >添加</el-button
      >
      </div>

      <el-card>
        <el-table
          :data="arr"
          style="width: 100%"
          height="539"
          stripe
          :header-cell-style="{ background: '#d2e0f9', color: '#606266' }"
          :cell-style="{'text-align':'center'}"
        >
          <el-table-column type="index" width="80" label="序号" align="center" />
          <el-table-column label="盒子编号"  align="center">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.boxNumbers }}</span>
            </template>
          </el-table-column>
          <el-table-column label="盒子IP" align="center">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.boxIp }}</span>
            </template>
          </el-table-column>
          <el-table-column label="盒子端口" align="center">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.boxPort }}</span>
            </template>
          </el-table-column>
          <el-table-column label="采购日期" align="center">
            <template slot-scope="scope">
              <span style="margin-left: 10px">{{ scope.row.procurementDate }}</span>
            </template>
          </el-table-column>
          <el-table-column
            property="status"
            label="盒子状态"
            align="center"
            header-align="center"
          >
            <template scope="scope">
              <el-switch
                v-model="scope.row.status"
                class="switch-btn"
                inactive-text="禁用"
                active-text="启用"
                :active-value="1"
                :inactive-value="2"
                active-color="#02538C"
                inactive-color="#B9B9B9"
                @change="changeSwitchs(scope.$index, scope.row)"
                disabled
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
              >
              <el-popover
                :ref="`popover-${scope.$index}`"
                trigger="click"
                placement="top"
                width="100"
              >
                <p class="el-icon-warning">确定删除</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="no(scope)"
                  >取消</el-button
                  >
                  <el-button type="primary" size="mini" @click="yes(scope)"
                  >确定</el-button
                  >
                </div>
                <el-button
                  slot="reference"
                  size="mini"
                  style="margin-left: 10px"
                  type="danger"
                >删除</el-button
                >
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
        <component-dialog
          :dialog-visible="dialogVisible"
          :dialog-info="dialogInfo"
          @update:dialogVisible="dialogVisibles"
          :show-close="false"
        />
        <div class="paging">
          <el-pagination
            :current-page="start"
            :page-size="PageSize"
            :pager-count="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-card>
    </div>
    <!-- switch -->
    <el-dialog title="状态更新" :visible.sync="dialogUpdata" class="mod_box1">
      <span>
        <i class="el-icon-warning" />此操作将会改变开关状态, 是否继续?</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="(dialogUpdata = false), cancelSwitch()"
          >取 消</el-button
        >
        <el-button
          type="primary"
          @click="(dialogUpdata = false), updataSwitch()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-dialog
      title="新增盒子信息"
      :visible.sync="dialogVisibleAdd"
      :showClose="false"
      :close-on-click-modal="false"
    >
      <el-form class="form" label-width="86px">
        <el-form-item label="编号: " label-width="91px">
          <el-input v-model="dialogInfoAdd.boxNumbers" class="bianji" />
        </el-form-item>
        <el-form-item label="IP: " label-width="91px">
          <el-input v-model="dialogInfoAdd.boxIp" class="bianji" />
        </el-form-item>
        <el-form-item label="端口: " label-width="91px">
          <el-input v-model="dialogInfoAdd.boxPort" class="bianji" />
        </el-form-item>
        <el-form-item label="采购日期: " label-width="91px">
          <el-date-picker
            v-model="dialogInfoAdd.procurementDate"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="状态 : ">
          <el-switch
            v-model="dialogInfoAdd.status"
            class="switch-btn"
            inactive-text="禁用"
            active-text="启用"
            :active-value="1"
            :inactive-value="2"
            active-color="#02538C"
            inactive-color="#B9B9B9"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog">取 消</el-button>
        <el-button type="primary" @click="saveDialog">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import componentDialog from "@/views/interactive/respiratorBoxDictionaryEdit";
import {
  respiratorBoxDictonaryInsert,
  respiratorBoxDictonaryDelete,
  respiratorBoxDictonaryQueryList, deparqueryList,
} from "@/api/table";

export default {
  components: {
    componentDialog,
  },
  data() {
    return {
      // 控制弹窗 显示
      dialogVisibleAdd:false,
      dialogVisible: false,
      dialogUpdata: false, // 开关
      switchId: "",
      switchStatus: "",
      // 点击查看按钮  这条数据详细信息
      dialogInfo: {},
      listLoading: true,
      loading: false,
      del: false,
      boxIp:"",
      boxNumbers:"",
      ruleForm: {
        bedCode: "",
        bedName: "",
        status: "",
      },
      options: [
        {
          id: "0",
          value: "1",
          label: "启用",
        },
        {
          id: "1",
          value: "2",
          label: "禁用",
        },
      ],
      dazuiwaList:[],
      value: "",
      arr: [],
      start: 1, // 默认显示第几页
      totalCount: 0, // 总条数，根据接口获取数据长度(注意：这里不能为空)
      pgeSizes: [10, 20, 30, 40], // 个数选择器（可修改）
      PageSize: 10, // 默认每页显示的条数（可修改）
      bedName:"",//床号
      bedList:[],//床位列表
      dialogVisibleBed:false,//床位选择弹窗
      monitorId:"",//监护仪id
      dialogInfoAdd: {
        id: "",
        status: "",
        boxNumbers: "",
        boxIp:"",
        procurementDate: ""
      },
    };
  },
  mounted() {
    this.respiratorBoxDictonaryQueryList(this.PageSize, this.start);
  },
  methods: {
    //删除
    yes(scope) {
      const id = scope.row.id;
      respiratorBoxDictonaryDelete({ id: id }).then((res) => {
        if (res.code === 200) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.respiratorBoxDictonaryQueryList(this.PageSize, this.start);
        }else{
          this.$message({
            message: "删除失败",
            type: "warning",
          });
        }
      });
      scope._self.$refs[`popover-${scope.$index}`].doClose();
    },
    //取消删除
    no(scope) {
      this.$message("取消删除");
      scope._self.$refs[`popover-${scope.$index}`].doClose();
    },
    // 搜索按钮
    btn() {
      this.start = 1;
      this.respiratorBoxDictonaryQueryList(this.PageSize, this.start);
    },
    //打开编辑页面
    handleEdit(info) {
      // 弹窗打开
      this.dialogVisible = true;
      this.dialogInfo = info;
    },
    // 子组件传 过来的 数据
    dialogVisibles(v) {
      this.dialogVisible = v;
      //刷新页面
      this.respiratorBoxDictonaryQueryList(this.PageSize, this.start);
    },
    //查询列表
    respiratorBoxDictonaryQueryList(PageSize, start) {
      respiratorBoxDictonaryQueryList({
        boxNumbers: this.boxNumbers,
        boxIp: this.boxIp,
        start: start,
        size: PageSize,
        status: this.value === "" ? null : Number(this.value),
      }).then((res) => {
        this.arr = res.data;
        this.totalCount = res.totalCount;
      });
    },
    // 每页显示的条数
    handleSizeChange(val) {
      // 改变每页显示的条数
      this.PageSize = val;
      // 点击每页显示的条数时，显示第一页
      this.respiratorBoxDictonaryQueryList(val, 1);
      // 注意：在改变每页显示的条数时，要将页码显示到第一页
      this.start = 1;
    },
    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.start = val;
      // 切换页码时，要获取每页显示的条数
      this.respiratorBoxDictonaryQueryList(this.PageSize, this.start);
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    cancelDialog() {
      this.dialogVisibleAdd = false;
    },
    //打开添加页面
    handleAdd() {
      // 弹窗打开
      this.dialogInfoAdd = {
        id: "",
        status: 1,
        boxNumbers: "",
        boxIp:"",
      };
      this.dialogVisibleAdd = true;

    },
    saveDialog() {
      respiratorBoxDictonaryInsert({
        id: this.dialogInfoAdd.id,
        status: this.dialogInfoAdd.status,
        boxNumbers: this.dialogInfoAdd.boxNumbers,
        boxIp: this.dialogInfoAdd.boxIp,
        boxPort: this.dialogInfoAdd.boxPort,
        procurementDate: this.dialogInfoAdd.procurementDate
      }).then((res) => {
        if (res.code === 200) {
          this.$message({
            message: "保存成功",
            type: "success",
          });
          this.respiratorBoxDictonaryQueryList(this.PageSize, this.start);
          this.dialogVisibleAdd = false;
        }else{
          this.$message({
            message: "保存失败",
            type: "warning",
          });
        }
      });
    }
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-dialog{
  width: 500px;
}
</style>
